/* 
Creates the figures and tables for the analysis of the CUT reform in the establishment sample
	// input: orbis_brasil_data_2012_2017, estlevel_dataset
	// output: 
		- profits_orbis, 
		- Table7.xlsx (sheets: profits_table)
*/ 

cap log close
cap log using "$logs/profits_log", replace


********
**DATA**
********

* Load Orbis data
import delimited "$raw\orbis_brasil_data_2012_2017.csv", clear
reshape long oprev pl pmargin, i(cnpj) j(year)

gen post = (year>=2015)

collapse (mean) oprev pl pmargin, by(cnpj post) fast

gen xxx = (oprev==.)&(pl==.)&(pmargin==.)
egen miss_info = max(xxx), by(cnpj)

unique cnpj
unique cnpj if (miss_info==1)
drop if miss_info==1
drop miss_info xxx

* Save
//estabid
gen fakeid_estab = subinstr(cnpj,"/","",.)
replace fakeid_estab = subinstr(fakeid_estab,".","",.)
replace fakeid_estab = subinstr(fakeid_estab,"-","",.)
destring fakeid_estab, replace
recast double fakeid_estab
//firm
gen fakeid_firm = substr(cnpj,1,10)
replace fakeid_firm = subinstr(fakeid_firm,".","",.)
destring fakeid_firm, replace
recast double fakeid_firm

order cnpj fakeid_estab fakeid_firm post
unique fakeid_estab
unique fakeid_firm
sort cnpj post, stable
save "$files\profits_orbis.dta", replace


**********************************************************************************
** IMPACT OF CUT REFORM ON ESTABLISHMENT LEVEL OUTCOMES (PANEL C: profit margin) **
**********************************************************************************

* Analysis sample (establishment level)
use "$files\estlevel_dataset.dta", clear
gen post = (year>=2015)
keep fakeid_estab post treat ind_mode municipality_mode bl_singest bl_bothFM bl_signing bl_covered
duplicates drop
merge m:1 fakeid_estab post using "$files\profits_orbis.dta"
keep if _merge==3
drop _merge

unique fakeid_estab
unique fakeid_estab if (bl_bothFM==1)&(bl_signing==1)&(bl_covered==1)
tab treat
tab treat if (bl_bothFM==1)&(bl_signing==1)&(bl_covered==1)

gen treatpost = treat * post
gen ind2d_num = floor(ind_mode/1e3)
egen indyrFE = group(post ind2d_num)
gen state_mode = floor(municipality_mode/1e4)
egen geogyrFE = group(post state_mode)

preserve

	keep if (bl_bothFM==1)&(bl_signing==1)&(bl_covered==1)

	foreach yyy of varlist pmargin {
		sum `yyy' if post==0 & treat==1
		local den = r(mean)
		reghdfe `yyy' treatpost, absorb(fakeid_estab *FE) cluster(fakeid_estab)
			su `yyy' if treat == 1 & post==0
			estadd scalar meandv=`r(mean)'
			estadd scalar obs=e(N)
			estimates store a1
		
	}

restore

#d ;
estout a1 using "$tables/profits_table.txt", 
style(tab) mlabels(none) label collabels(none)
cells(b(star fmt(%9.3f)) se(par)) 
stats(meandv obs, fmt(%9.3fc %9.0fc ) labels("mean depvar" "N" )) 
drop(o.*, relax) keep(treatpost, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
#d cr

import delimited "$tables/profits_table.txt", clear
rename v1 coeffs
rename v2 pmargin
export excel using "$tables/Table7.xlsx", sheet("profits_table") cell(A1) firstrow(var) sheetreplace


cap log close
